package org.cttic.visualization.visualization.z_service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.cttic.visualization.visualization.common.IdentifyPointInfo;
import org.cttic.visualization.visualization.common.MarkSearchRequest;
import org.cttic.visualization.visualization.common.MarkSearchResponse;
import org.cttic.visualization.visualization.util.ResponseObject;
import org.cttic.visualization.visualization.z_service.MarkSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MarkSearchServerImpl implements MarkSearchService {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public ResponseObject idePointList(MarkSearchRequest request) {
        ResponseObject response = ResponseObject.success();

        if ("1".equals(request.getDataType())) {
            log.info("查询普通门架信息:{}", request.toString());
            response.setBizBody(gantryInfos(request, 1));
        } else if ("2".equals(request.getDataType())) {
            log.info("查询省界门架信息:{}", request.toString());
            response.setBizBody(gantryInfos(request, 2));
        } else if ("3".equals(request.getDataType())) {
            log.info("查询收费站信息:{}", request.toString());
            response.setBizBody(stationInfos(request));
        } else {
            return response.error("未知的识别点类型。");
        }

        return response;
    }


    /**
     * 门架
     *
     * @param request
     * @param gantryType
     * @return
     */
    private JSONObject gantryInfos(MarkSearchRequest request, int gantryType) {
        JSONObject jsonObject = new JSONObject();
        List<MarkSearchResponse> provinceList = new ArrayList<>();
        String[] split = request.getProvinceId().split("\\|");
        String dataValue = request.getDataValue();
        for (String s : split) {

            //模糊查询条件
            String dataValueSql = " and (gantryId like '%" + dataValue + "%' or gantryName like '%" + dataValue + "%')";


            String sql = " SELECT gantryId gantryId,gantryName,gantryType type " +
                "FROM T_GANTRY_MOUNTING WHERE isAbnormal = 0  and provinceId = '" + s + "' and gantryType =" + gantryType;

            String countSql = "SELECT COUNT(id) from `T_GANTRY_MOUNTING` where isAbnormal = 0 and provinceId = " + s;

            if (StrUtil.isNotEmpty(dataValue)) {
                countSql += dataValueSql;
                sql += dataValueSql;
            }

            Integer totalCount = jdbcTemplate.queryForObject(countSql, Integer.class);

            if (totalCount == 0) {
                MarkSearchResponse response1 = new MarkSearchResponse();
                response1.setProvinceId(s);
                response1.setDataType(gantryType+"");
                provinceList.add(response1);
                continue;
            }

            int pageSize = request.getPageSize();

            int totalPage = totalCount / pageSize;

            if (totalCount % pageSize != 0)
                totalPage++;


            int pageNo = request.getPageNo().intValue();

            int start = (pageNo - 1) * request.getPageSize();


            sql += " order by gantryId asc limit "+start+"," + pageSize;


            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);

            MarkSearchResponse mark = new MarkSearchResponse();
            mark.setDataType(request.getDataType());
            mark.setProvinceId(s);
            mark.setDataList(maps);
            mark.setPageNo(pageNo);
            mark.setPageSize(pageSize);
            mark.setTotalCount(totalCount);
            mark.setTotalPage(totalPage);

            provinceList.add(mark);
        }


        jsonObject.put("provinceList", provinceList);
        return jsonObject;
    }

    /**
     * 收费站
     *
     * @param request
     * @return
     */
    private JSONObject stationInfos(MarkSearchRequest request) {
        JSONObject jsonObject = new JSONObject();
        List<MarkSearchResponse> provinceList = new ArrayList<>();
        String[] split = request.getProvinceId().split("\\|");
        String dataValue = request.getDataValue();
        for (String s : split) {
            //模糊查询条件
            String dataValueSql = " and (stationId like '%" + dataValue + "%' or stationName like '%" + dataValue + "%')";


            String sql = "SELECT stationId gantryId,stationName gantryName, '3' pointType " +
                "FROM `T_STATION` where isAbnormal = 0 and provinceId = " + s;


            String countSql = "SELECT COUNT(id) from `T_STATION` where isAbnormal = 0 and provinceId = " + s;

            if (StrUtil.isNotEmpty(dataValue)) {
                countSql += dataValueSql;
                sql += dataValueSql;
            }


            Integer totalCount = jdbcTemplate.queryForObject(countSql, Integer.class);

            if (totalCount == 0) {
                MarkSearchResponse response1 = new MarkSearchResponse();
                response1.setProvinceId(s);
                response1.setDataType("3");
                provinceList.add(response1);
                continue;
            }

            int pageSize = request.getPageSize();

            int totalPage = totalCount / pageSize;

            if (totalCount % pageSize != 0)
                totalPage++;


            int pageNo = request.getPageNo().intValue();

            int start = (pageNo - 1) * request.getPageSize();


            sql += " order by gantryId asc limit "+start+"," + pageSize;


            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);

            MarkSearchResponse mark = new MarkSearchResponse();
            mark.setDataType(request.getDataType());
            mark.setProvinceId(s);
            mark.setDataList(maps);
            mark.setPageNo(pageNo);
            mark.setPageSize(pageSize);
            mark.setTotalCount(totalCount);
            mark.setTotalPage(totalPage);

            provinceList.add(mark);
        }
        jsonObject.put("idePointList", provinceList);
        return jsonObject;
    }


}
